﻿using System;
using System.Collections.Generic;
using System.Data.Objects;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using VNEYEModel;

public partial class Account_Service : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if(!IsPostBack)
        {
            LoadData();
            gvServices.DataBind();
        }
    }
    public void LoadData()
    {
        var vn = new VNEYEEntities();
        var services = vn.getAllServices();
        gvServices.DataSource = services;
    }
    protected void btnAdd_Click(object sender, EventArgs e)
    {
        Response.Redirect("AddService.aspx");
    }
    protected void gvServices_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        LoadData();
        gvServices.PageIndex = e.NewPageIndex;
        gvServices.DataBind();
    }
    protected void gvServices_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if(e.CommandName=="Detail")
        {
            int id = int.Parse(e.CommandArgument.ToString());
            Response.Redirect("ServiceDetail.aspx?id="+id);
        }
    }
    private List<int> GetCheck()
    {
        List<int> list = new List<int>();
        for (int i = 0; i < gvServices.Rows.Count; i++)
        {
            GridViewRow row = gvServices.Rows[i];
            CheckBox isCheck = ((CheckBox)row.FindControl("cbSelect"));
            if (isCheck.Checked)
            {
                int id = (int)gvServices.DataKeys[i].Value;
                list.Add(id);
            }

        }
        return list;
    }
    protected void btnStatus_Click(object sender, EventArgs e)
    {
        List<int> check = GetCheck();
        if(check.Count<=0)
        {
            ScriptManager.RegisterStartupScript(this, this.GetType(), "Alert", "alert('Bạn Chưa Chọn Dịch Vụ Nào');",true);
        }
        else
        {
            foreach (int i in check)
            {
                var vn = new VNEYEEntities();
                var result = vn.getServiceById(i);
                foreach (Service service in result)
                {
                    if(service.STATUS==false)
                    {
                        vn.updateStatusServiceById(i, true);
                    }
                    else
                    {
                        vn.updateStatusServiceById(i, false);
                    }
                }
            }
            ScriptManager.RegisterStartupScript(this, this.GetType(), "Alert", "alert('Thay Đổi Thành Công');", true);
            LoadData();
            gvServices.DataBind();
        }
    }
}